import { Notify, date } from 'quasar'
import { mapMutations } from 'vuex'

const baseApi = 'http://quasar.soonclick.com/api/'
const baseSrc = 'http://quasar.soonclick.com/'
const baseImgUploadURL = baseApi + 'uploadfiles/uploadIMG/'
const baseFileUploadURL = baseApi + 'uploadfiles/uploadFiles/'
const baseBaiduApi = 'http://api.map.baidu.com/'
export default {
  // 默认请求基础路径
  // defaultBaseURL: 'http://192.168.10.142:8889/api/',
  defaultBaseURL: baseApi,
  // 默认上传图片请求基础路径
  defaultImgUploadURL: baseImgUploadURL,
  // 上传后的图片调用基础路径
  defaultImgSrc: baseSrc,
  // 上传后的文件调用基础路径
  defaultFileSrc: baseSrc,
  // 百度根据IP定位API地址
  baiduIPLocationAPI: baseBaiduApi + 'location/ip',
  // 百度地理编码API地址
  baiduGeoCodingAPI: baseBaiduApi + 'geocoding/v3/',

  // 主题色
  styleGroup: [
    {
      name: '默认',
      highlight: {
        logoWhite: false,
        dark: false,
      },
      iconStyle: {
        style: 'background-color: #f1f1f1; border: 1px solid #ddd',
        color: '#1976d2',
        checked: true
      },
      palette: {
        primary: '#1976d2',
        secondary: '#039be5',
        accent: '#01579b',
        dark: '#424242',
        positive: '#7cb342',
        negative: '#c62828',
        info: '#bbdefb',
        warning: '#ffb300',
      }
    },
    {
      name: '露草',
      highlight: {
        logoWhite: true,
        dark: true,
      },
      iconStyle: {
        style: 'background-color: #35acfd',
        color: '#35acfd',
        checked: false
      },
      palette: {
        primary: '#329acf',
        secondary: '#4eb0d4',
        accent: '#2481bf',
        dark: '#35acfd',
        positive: '#47b598',
        negative: '#d9294f',
        info: '#ffda91',
        warning: '#eb9346',
      }
    },
    {
      name: '冰绿',
      highlight: {
        logoWhite: true,
        dark: true,
      },
      iconStyle: {
        style: 'background-color: #06b5b0',
        color: '#06b5b0',
        checked: false
      },
      palette: {
        primary: '#169da2',
        secondary: '#50babe',
        accent: '#006e71',
        dark: '#06b5b0',
        positive: '#539f54',
        negative: '#ae3844',
        info: '#98e2e5',
        warning: '#eb9346',
      }
    },
    {
      name: '春绿',
      highlight: {
        logoWhite: true,
        dark: true,
      },
      iconStyle: {
        style: 'background-color: #21a775',
        color: '#21a775',
        checked: false
      },
      palette: {
        primary: '#4f9676',
        secondary: '#65bf94',
        accent: '#096944',
        dark: '#21a775',
        positive: '#28bb64',
        negative: '#c22547',
        info: '#86e0b8',
        warning: '#eb9346',
      }
    },
    {
      name: '琥珀',
      highlight: {
        logoWhite: true,
        dark: true,
      },
      iconStyle: {
        style: 'background-color: #faa64b',
        color: '#faa64b',
        checked: false
      },
      palette: {
        primary: '#ed8f2a',
        secondary: '#de892e',
        accent: '#b96000',
        dark: '#faa64b',
        positive: '#21a775',
        negative: '#dc3022',
        info: '#f4ceaf',
        warning: '#ed7236',
      }
    },
    {
      name: '樱花',
      highlight: {
        logoWhite: true,
        dark: true,
      },
      iconStyle: {
        style: 'background-color: #f96868',
        color: '#f96868',
        checked: false
      },
      palette: {
        primary: '#e75c6f',
        secondary: '#ed7484',
        accent: '#b73247',
        dark: '#f96868',
        positive: '#8aad68',
        negative: '#aa3047',
        info: '#fbc4cb',
        warning: '#ed7236',
      }
    },
    {
      name: '霓虹',
      highlight: {
        logoWhite: true,
        dark: true,
      },
      iconStyle: {
        style: 'background-color: #c24e99',
        color: '#c24e99',
        checked: false
      },
      palette: {
        primary: '#b02082',
        secondary: '#ca2581',
        accent: '#9a2981',
        dark: '#c24e99',
        positive: '#a1b76f',
        negative: '#e02a5b',
        info: '#febdd9',
        warning: '#f0b012',
      }
    },
    {
      name: '鸦青',
      highlight: {
        logoWhite: true,
        dark: true,
      },
      iconStyle: {
        style: 'background-color: #465161',
        color: '#465161',
        checked: false
      },
      palette: {
        primary: '#75878b',
        secondary: '#697277',
        accent: '#4e5357',
        dark: '#465161',
        positive: '#677961',
        negative: '#bf2b21',
        info: '#febdd9',
        warning: '#ffa630',
      }
    },
  ],

  // table动态分页
  dynamicPagination (props) {
    const { rowsNumber, rowsPerPage } = props.pagination
    let newRowsPerPage
    if (rowsPerPage === 0) {
      newRowsPerPage = rowsNumber
    } else {
      newRowsPerPage = rowsPerPage
    }
    return newRowsPerPage
  },
  // 格式化form数据为对象格式的Json字符串
  formatFormData (evt) {
    const formData = new FormData(evt.target)
    const newRowData = []
    const formatFormData = {}

    for (const [name, value] of formData.entries()) {
      newRowData.push({
        name,
        value
      })
    }
    newRowData.forEach((item, index) => {
      formatFormData[item.name] = item.value
    })

    return formatFormData
    // 转换为Json字符串
    // return JSON.stringify(formatFormData)
  },

  // notify全局定义
  notify (message, color) {
    Notify.create({
      message,
      color,
      position: 'center',
      timeout: 2000
    })
  },

  // 获取当前日期，格式化为 YYYY/MM/DD 格式
  initDate (_date, start, length) {
    let formattedDate = date.formatDate(_date, 'YYYY-MM-DD HH:mm:ss')
    let dateNow = formattedDate.substring(start, length)

    return dateNow
  },

  // 上传器上传图片地址、方法的工厂函数
  uploadedIMG () {
    return {
      url: baseImgUploadURL,
      method: 'POST'
    }
  },

  // 上传器上传文件地址、方法的工厂函数
  uploadedFile () {
    return {
      url: baseFileUploadURL,
      method: 'POST'
    }
  },

  // 上传器上传图片返回信息提取图片地址格式化
  formatUploadedInfo (info) {
    const _ImgData = JSON.parse(info.xhr.response)
    const _Img = []
    _ImgData.data.forEach(item => {
      _Img.push(
        item.url
      )
    })

    return _Img
  },

  // 获取文件存储地址，根据扩展名，给出相应图标和颜色
  buildDocIcon (docName) {
    let _extensionName = docName.substring(docName.lastIndexOf(".") + 1)
    let _docIcon = ''
    if (_extensionName === 'doc' || _extensionName === 'docx') {
      _docIcon = 'word'
    } else if (_extensionName === 'xls' || _extensionName === 'xlsx') {
      _docIcon = 'excel'
    } else if (_extensionName === 'ppt' || _extensionName === 'pptx') {
      _docIcon = 'powerpoint'
    } else if (_extensionName === 'pdf') {
      _docIcon = 'pdf'
    }
    return _docIcon
  },

  // 返回上一页
  returnPrevPage () {
    // 这个判断用来解决这种情况，当用户没有上一条路由的历史记录，出现点击返回按钮没有反应时，下面的代码用来判断有没有上一条路由的历史记录   如果没有则返回首页
    if (window.history.length <= 1) {
      this.$router.push({ path: "/" });
      return false;
    } else {
      this.$router.go(-1);
    }
  },

  // 递归循环过滤路由数组，生成动态菜单
  filterNavData (arryRoutes, arryRoles) {
    return arryRoutes.filter(item => {
      if (item.children) {
        item.children = this.filterNavData(item.children, arryRoles)
        return item
      } else {
        return arryRoles.indexOf(item.name) > -1
      }
    }).map(item => {
      item = Object.assign({}, item)
      return item
    })
  },


  // 深拷贝数组，包含对象值为function
  deepcopyArry (obj) {
    if (obj == null) { return null }
    var result = Array.isArray(obj) ? [] : {};
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (typeof obj[key] === 'object') {
          result[key] = this.deepcopyArry(obj[key]); // 如果是对象，再次调用该方法自身 
        } else {
          result[key] = obj[key];
        }
      }
    }
    return result;
  }
  // session过期操作
  // ...mapMutations(['changeLogin']),
  // unauthorized () {
  //   // 弹出警告框
  //   Notify.create({
  //     message: '身份信息过期',
  //     color: 'red-8',
  //     position: 'center',
  //     timeout: 2000
  //   })
  //   // 清除vuex中的token
  //   this.changeLogin({ Authorization: '' })
  //   // 路由跳转
  //   this.$router.push('/Login', onComplete => { }, onAbort => { });
  // }

}
